const { mix } = require("laravel-mix");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const webpack = require("webpack");

const path = require("path");
const JsOutputPath = "public/js";
const CssOutputPath = "public/css";
const VendorJsPath = "public/js/vendor.js";
const AppRoot = "./app/Winged/";

const VendorComponents = ["vue", "axios", "element-ui"];

var isDev = false; //process.env.NODE_ENV == 'development'

// 将各个vue文件里的css提取出来，以link的形式手动引入
mix.config.options.extractVueStyles = "[name].css";
//将各个vue文件里的css提取出来，以link的形式引入 end

mix.webpackConfig({
	output: {
		publicPath: "/",
		/*这两项需要动态添加名称*/
		filename: isDev ? "[name].js" : "[name].[chunkhash].js",
		chunkFilename: isDev ? "js/[name].js" : "js/[name].[chunkhash].js"
	},
	module: {
		rules: [
			{
				test: /\.css$/,
				loader: "style-loader!css-loader"
			},
			{
				test: /\.less$/,
				loader: "style-loader!css-loader!less-loader"
			},
			{
				test: /\.(jpg|png|jpeg|gif)$/,
				loader: "file-loader",
				query: {
					name: "[name].[ext]?[hash]",
					outputPath: "images/"
				}
			}
		]
	},
	plugins: [],
	resolve: {
		alias: {
			winged: path.resolve(__dirname, "app/Winged/Resources/"),
			lib: path.resolve(__dirname, "app/Winged/lib/")
		}
	},
	// ali-oss的两个依赖库，已在npm中不可用，所以加上'empty'
	node: {
		fs: "empty",
		child_process: "empty"
	}
});

mix.extract(VendorComponents, VendorJsPath);

/*
 * 从指定模块目录导入资源文件
 * resourceType：js 或者 sass
 */
function importModuleResources(
	moduleResourcePath,
	files,
	resourceType,
	modulePrefix
) {
	if (files !== undefined) {
		for (let i = 0; i < files.length; i++) {
			let file = files[i];
			if (resourceType == "js") {
				mix.js(
					moduleResourcePath + "js/" + file,
					JsOutputPath + "/" + modulePrefix + file
				);
			} else if (resourceType == "less") {
				mix.less(
					moduleResourcePath + "less/" + file,
					CssOutputPath +
						"/" +
						modulePrefix +
						file.replace(".less", ".css")
				);
			} else {
				// unsupported type.
			}
		}
	}
}

/*
 * 按照目录和命名约定，从指定模块载入所有js/sass等资源文件
 */
function importModule(module) {
	var resourcesPath = AppRoot + module + "/Resources/";
	var modules = require(resourcesPath + "webpack.mix");
	var modulePrefix = module.toLowerCase() + "-";
	importModuleResources(resourcesPath, modules.js, "js", modulePrefix);
	importModuleResources(resourcesPath, modules.less, "less", modulePrefix);
}

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.styles(
	[
		AppRoot + "Resources/css/reset.css",
		AppRoot + "Resources/css/AdminLTE.min.css"
	],
	CssOutputPath + "/winged.css"
);

// 加载模块
importModule("Patients");
//开发Assessment模块，请取消注释，并注释掉patients模块
importModule("Evaluation");
importModule("Common");

if (!mix.config.inProduction) {
	mix.browserSync("localhost");
} else {
	mix.version();
}
